<?php
// +----------------------------------------------------------------------
// | Author: Sam
// +----------------------------------------------------------------------
namespace plugins\web\controller;
use cmf\controller\PluginAdminBaseController;
use app\admin\model\ThemeModel;
use think\Db;

/**
 * Class AdminIndexController
 * @package plugins\web\controller
 * @adminMenuRoot(
 *     'name'   =>'站点管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 1000,
 *     'icon'   =>'dashboard',
 *     'remark' =>'设置入口'
 * )
 */
class AdminIndexController extends PluginAdminBaseController
{


    protected function _initialize()
    {
        parent::_initialize();
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        }
    }


    /**
     * 站点设置
     * @adminMenu(
     *     'name'   => '站点设置',
     *     'parent' => 'default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 1000,
     *     'icon'   => '',
     *     'remark' => '站点设置',
     *     'param'  => ''
     * )
     */
    public function index()
    {
		$datalist = Db::name('plugin_web')->order("id DESC")->paginate(20);
        $page = $datalist->render();
        $this->assign("datalist", $datalist);
        $this->assign("page", $page);
        return $this->fetch('index');
    }
    public function edit()
    {
		$id = $this->request->param('id', 0, 'intval');
		$data = Db::name('plugin_web')->where(["id" => $id])->find();
		$this->assign("data", $data);
		$themeModel = new ThemeModel();
		$themes     = $themeModel->select();
		$this->assign("themes", $themes);
        return $this->fetch('edit');
    }
    public function edit_post()
    {
        $_POST = $this->request->param();
		if(empty($_POST['web_url'])) $this->error("域名不能为空！");
		if(empty($_POST['pc_theme'])) $this->error("电脑端模板不能为空！");
		$data  = [
                'web_url'        	=> $_POST['web_url'],
                'pc_theme'        	=> $_POST['pc_theme'],
                'mob_theme'        	=> $_POST['mob_theme'],
                'status'        	=> $_POST['status'],
                'msg'        		=> $_POST['msg'],
                'update_time'  		=> time(),
            ];
		if(empty($_POST['id'])){
			$data['create_time'] = $data['update_time'];
			$id = Db::name('plugin_web')->insertGetId($data);
		}else{
			Db::name('plugin_web')->where('id', $_POST['id'])->update($data);
			$id = $_POST['id'];
		}
        $this->success("提交成功！",cmf_plugin_url('Web://AdminIndex/edit',['id'=>$id]));
    }
    //删除
	function delete()
	{
		$_POST = $this->request->param();
		Db::name('plugin_web')->where('id', $_POST['id'])->delete();
		$this->success('删除成功！');
	}


}